package com.example.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.example.common.Result;
import com.example.dto.UserDTO;
import com.example.entity.UserEntity;
import com.example.service.UserService;
import com.example.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/auth")
public class AuthController {
    @Autowired
    private UserService userService;


    @GetMapping
    public Object loginInfo() {
        return Result.success(StpUtil.getTokenInfo());
    }

    @PostMapping
    public Object login(@RequestBody UserDTO userDTO) {
        UserVO UserVo = userService.login(userDTO);
        System.out.println(UserVo);
        if (UserVo != null) {
            StpUtil.login(UserVo.getId());
            SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
            return Result.success(tokenInfo);
        } else {
            return Result.error("用户名或密码错误");
        }
    }
}
